package tankgame04;

import java.io.*;
import java.util.Vector;

/**
 * 该类用于记录相关信息和文件的交互
 */
public class Recorder {
    //定义变量，记录我方坦克击毁敌方坦克的数量
    private static int killEnemyTankNum = 0;
    //定义IO对象，用于写数据到文件中
    private static BufferedWriter bw = null;
    private static BufferedReader br = null;
    private static String recordFile = "src\\myRecord.txt";
    //定义一个Node的Vector，用于保存敌人的信息 node
    private static Vector<Node> nodes = new Vector<>();

    //定义Vector，指向 MyPanel 对象的 敌人坦克Vector
    private static Vector<EnemyTank> enemyTanks = null;

    //编写方法，用于读取 recordFile 文件，恢复相关信息
    //该方法在选择继续上局游戏的时候调用
    public static Vector<Node> getNodesAndEnemyTankRec() {
        try {
            br = new BufferedReader(new FileReader(recordFile));
            killEnemyTankNum = Integer.parseInt(br.readLine());
            //循环读取文件，生成nodes集合
            String line = "";
            while ((line = br.readLine()) != null) {
                String[] xyd = line.split(" ");
                Node node =new Node (Integer.parseInt(xyd[0]),
                        Integer.parseInt(xyd[1]),Integer.parseInt(xyd[2]));
                nodes.add(node);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return nodes;
    }


    //编写方法，当游戏退出时，将 killEnemyTankNum 保存到 recordFile
    //还可以保存敌人坦克的坐标和方向
    public static void keepRecord() {
        try {
            bw = new BufferedWriter(new FileWriter(recordFile));
            bw.write(killEnemyTankNum + "");
            bw.newLine();//换行
            //遍历敌人坦克的Vector，根据情况进行保存
            //OOP，定义一个属性，然后通过set得到敌人坦克的Vector
            for (int i = 0; i < enemyTanks.size(); i++) {
                EnemyTank enemyTank = enemyTanks.get(i);
                if (enemyTank.isLive) {
                    //保存该enemyTank的信息
                    String record = enemyTank.getX() + " " + enemyTank.getY() + " " +
                            enemyTank.getDirection();
                    //写入到文件
                    bw.write(record);
                    bw.newLine();
                }
            }

            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    //当我方坦克击毁一个敌方坦克时
    public static void addKillEnemyTankNum() {
        Recorder.killEnemyTankNum++;
    }

    public static int getKillEnemyTankNum() {
        return killEnemyTankNum;
    }

    public static void setKillEnemyTankNum(int killEnemyTankNum) {
        Recorder.killEnemyTankNum = killEnemyTankNum;
    }

    public static void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        Recorder.enemyTanks = enemyTanks;
    }

    //返回记录文件的路径
    public static String getRecordFile() {
        return recordFile;
    }
}
